<?php

namespace App\Models;

use Dcat\Admin\Admin;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Config;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use HasDateTimeFormatter;
    use SoftDeletes;
    use HasFactory;
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'company_id',
        'username',
        'realname',
        'avatar',
        'email',
        'password',
        'ip',
        'status',
        'review_status',
        'phone',
        'payee_phone',
        'last_login_time',
        'id_number',
        'sex',
        'birthday',
        'zip_code',
        'province_id',
        'city_id',
        'district_id',
        'unit',
        'unit_level',
        'department',
        'title',
        'bank_number',
        'bank_account_name',
        'bank_opening',
        'bank_account_open_local',
        'agent',
        'agent_code',
    ];



    public function auth()
    {
        return $this->hasOne(UsersAuth::class);
    }

    public function projectName()
    {
        return $this->belongsToMany(Project::class);
    }

    public function projectCode()
    {
        return $this->belongsToMany(Project::class);
    }

    public function adminUser()
    {
        return $this->belongsTo(AdminUser::class, 'agent', 'id');
    }


    // /**
    //  * 追加到模型数组表单的访问器
    //  *
    //  * @var array
    //  */
    // protected $appends = ['number'];

    // public function getNumberAttribute()
    // {
    //     return $this->attributes['number'] = Config::get('打卡目标次数');
    // }

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
        // 'realname',
        // 'avatar',
        // 'email',
        'email_verified_at',
        // 'created_at',
        // 'updated_at',
        'deleted_at',
        // 'ip',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        // 'email_verified_at' => 'datetime',
    ];

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims(): array
    {
        return [];
    }

    //判断该手机号是否注册
    public static function checkPhone($phone = '')
    {
        return self::where('username', $phone)->orWhere('phone', $phone)->count();
    }

    public static function getDoctor()
    {
        $admin = Admin::user();

        $query = function ($query) use ($admin) {
            if ($admin->isRole('regional-supervisor')) {
                // 查询自己区域的代理人id, 'admin_id' => $admin->id
                $agent = AdminUser::where(['region' => $admin->region, 'status' => 1])->pluck('id');
                $query->whereIn('agent', $agent);
            }
            // 如果是代理人的话查看自己创建的用户
            if ($admin->isRole('agent')) {
                $query->where('agent', $admin->id);
            }
        };


        $list = self::where($query)->where(['company_id' => $admin->company_id])->select('id', 'realname', 'phone')->get();
        foreach ($list as $key => &$val) {
            $val['realname'] = $val['realname'] . '-' . $val['phone'];
        }
        return $list->pluck('realname', 'id');
    }
}
